package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResCode;
import com.fhnew.app.model.network.entity.req.Resregister;
import com.fhnew.app.util.Md5;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class RegisterDialog extends Activity {

    ImageView close;
    TextView titletext;
    EditText rigistrealname;
    LinearLayout fullnamelayout;
    EditText rigistusername;
    EditText rigistpass;
    TextView phonename;
    EditText rigistphone;
    LinearLayout rigistphonelayout;
    EditText rigistidcode;
    ImageView codeImg;
    TextView gorigist;


    private String username;
    private String password;
    private String password2;
    private String realname;
    private String phone;
    private String code;
    private String codes;

    /**
     * 成员注册
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_dialog);
        //获取验证码     和注册同一个接口，获取验证码是get请求
        getcode();
        close = findViewById(R.id.close);
        titletext = findViewById(R.id.titletext);
        rigistrealname = findViewById(R.id.rigistrealname);
        fullnamelayout = findViewById(R.id.fullnamelayout);
        rigistusername = findViewById(R.id.rigistusername);
        rigistpass = findViewById(R.id.rigistpass);
        phonename = findViewById(R.id.phonename);
        rigistpass = findViewById(R.id.rigistpass);
        rigistphone = findViewById(R.id.rigistphone);
        rigistphonelayout = findViewById(R.id.rigistphonelayout);
        rigistpass = findViewById(R.id.rigistpass);
        rigistidcode = findViewById(R.id.rigistidcode);
        codeImg = findViewById(R.id.code_img);
        gorigist = findViewById(R.id.gorigist);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        codeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刷新验证码
                getcode();
            }
        });
        gorigist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //去注册
                username = rigistusername.getText().toString().trim();//账号
                password = rigistpass.getText().toString().trim();//密码
                realname = rigistrealname.getText().toString().trim();//真实姓名
                phone = rigistphone.getText().toString().trim();//手机号
                code = rigistidcode.getText().toString().trim();//用户输入的验证码
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(RegisterDialog.this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterDialog.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,}$")) {
                    Toast.makeText(RegisterDialog.this, "您的密码不符合规定", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(RegisterDialog.this, "请输入验证码", Toast.LENGTH_LONG).show();
                    return;
                }

                String password1 = Md5.toMd5(password);
                register(username, password1, realname, phone, "", codes, code);
            }
        });

    }

    private void getcode() {
        HttpJsonUtils.getHttpApiInstance()
                .getcode(Constants.getuseAdress() + "api/m/pc/bet/insertUser")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCode>() {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResCode resCode) {
                        if (null != resCode.data) {
                            switch (resCode.data.register_type) {
                                case 0:
                                    rigistphonelayout.setVisibility(View.GONE);
                                    break;
                                case 1:
                                    rigistphonelayout.setVisibility(View.VISIBLE);
                                    rigistphone.setHint("11位手机号");
                                    phonename.setText("手机号");
                                    break;
                                case 2:
                                    rigistphonelayout.setVisibility(View.VISIBLE);
                                    rigistphone.setHint("请输入qq号");
                                    phonename.setText("qq号");
                                    break;
                                case 3:
                                    rigistphonelayout.setVisibility(View.VISIBLE);
                                    rigistphone.setHint("请输入微信号");
                                    phonename.setText("微信号");
                                    break;
                                default:
                                    Toast.makeText(RegisterDialog.this, "验证码接口返回有误", Toast.LENGTH_SHORT).show();
                                    break;
                            }
                            if (resCode.data.is_reg_fullname == 2) {
                                fullnamelayout.setVisibility(View.GONE);
                            }
                            codes = "";
                            codes = resCode.data.captcha_code;
                            Bitmap bitmap = null;
                            byte[] bitmapArray;
                            bitmapArray = Base64.decode(resCode.data.code, Base64.NO_WRAP);
                            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
                            codeImg.setImageBitmap(bitmap);//显示图片
                        }
                    }
                });
    }


    private void register(String username, String password1, String realname, String phone, String peuser, String codes, String code) {
        HttpJsonUtils.getHttpApiInstance()
                .register(Constants.getuseAdress() + "api/m/pc/bet/insertUser", username, password1, realname, phone, "", codes, code, peuser, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resregister>() {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        getcode();
                    }

                    @Override
                    protected void onCustomNext(Resregister resregister) {
                        if (resregister.code != 0) {
                            Toast.makeText(RegisterDialog.this, resregister.msg, Toast.LENGTH_SHORT).show();
                            getcode();
                        } else {
                            Toast.makeText(RegisterDialog.this, "注册成功", Toast.LENGTH_LONG).show();
                            finish();
                        }
                    }
                });
    }
}
